#!/usr/bin/python3
# _*_ coding: utf-8 _*_
#
# Copyright (C) 2024 - 2024 heihieyouheihei, Inc. All Rights Reserved 
#
# @Time    : 2024/9/12 15:50
# @Author  : Yuyun
# @File    : 层序遍历列表构建二叉树.py
# @IDE     : PyCharm
class TreeNode:
    def __init__(self, val, left = None, right = None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    #   依据层序遍历列表，构造二叉树，有NULL值
    def bulidtree_levelorder(self, levelorder):
        if not levelorder:
            return None
        root = TreeNode(levelorder[0])
        queue = [root]
        i = 1
        #   层序遍历，节点值从左到右添加
        while queue and i < len(levelorder):
            #   队列第1位元素
            node = queue.pop(0)
            # 检查左子节点
            if levelorder[i] is not None:
                node.left = TreeNode(levelorder[i])
                queue.append(node.left)
            i += 1
            # 检查右子节点
            if i < len(levelorder)and levelorder[i] is not None:
                node.right = TreeNode(levelorder[i])
                queue.append(node.right)
            i += 1
        return root